package com.atguigu.gulimall.member.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.common.utils.HttpUtils;
import com.atguigu.gulimall.member.dao.MemberLevelDao;
import com.atguigu.gulimall.member.entity.MemberLevelEntity;
import com.atguigu.gulimall.member.exception.PhoneExistException;
import com.atguigu.gulimall.member.exception.UsernameExistException;
import com.atguigu.gulimall.member.vo.MemberLoginVo;
import com.atguigu.gulimall.member.vo.MemberRegistVo;
import com.atguigu.gulimall.member.vo.SocialUser;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.member.dao.MemberDao;
import com.atguigu.gulimall.member.entity.MemberEntity;
import com.atguigu.gulimall.member.service.MemberService;


@Service("memberService")
public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> implements MemberService {

    @Autowired
    MemberLevelDao memberLevelDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MemberEntity> page = this.page(
                new Query<MemberEntity>().getPage(params),
                new QueryWrapper<MemberEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 用户注册功能
     * @param vo
     */
    @Override
    public void regist(MemberRegistVo vo) {
        MemberDao memberDao = this.baseMapper;
        MemberEntity entity = new MemberEntity();

        //设置默认等级
       MemberLevelEntity levelEntity = memberLevelDao.getDefaultLevel();
        entity.setLevelId(levelEntity.getId());

        //检查手机号和用户名是否唯一， 为了让controller能感知到异常，异常机制处理
        checkPhoneUnique(vo.getPhone());
        checkUsernameUnique(vo.getUserName());

        //设置手机号
        entity.setMobile(vo.getPhone());
        entity.setUsername(vo.getUserName());
        //设置用户昵称
        entity.setNickname(vo.getUserName());
        //设置密码 密码要进行加密存储
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String encode = passwordEncoder.encode(vo.getPassword());
        entity.setPassword(encode);
        //其他的默认信息

        //保存
        memberDao.insert(entity);
    }

    @Override
    public void checkPhoneUnique(String phone) throws PhoneExistException {
        MemberDao memberDao = this.baseMapper;
        Integer count = memberDao.selectCount(new QueryWrapper<MemberEntity>().eq("mobile", phone));
        if(count > 0){
            throw new PhoneExistException();
        }
    }

    @Override
    public void checkUsernameUnique(String username) throws UsernameExistException{
        MemberDao memberDao = this.baseMapper;
        Integer count = memberDao.selectCount(new QueryWrapper<MemberEntity>().eq("username", username));
        if(count > 0){
            throw new UsernameExistException();
        }
    }

    /**
     * 普通用户登录功能
     * @param vo
     * @return
     */
    @Override
    public MemberEntity login(MemberLoginVo vo) {
        String loginacct = vo.getLoginacct();
        String password = vo.getPassword(); //123456

        //1.去数据库查询 SELECT * FROM `ums_member` WHERE username = ? OR mobile = ？
        MemberDao baseMapper = this.baseMapper;
        MemberEntity entity = baseMapper.selectOne(new QueryWrapper<MemberEntity>()
                .eq("username", loginacct)
                .or().eq("mobile", loginacct));

        if(entity == null){
            //登录失败
            return null;
        }else{
            // 1.获取到数据库的password $2a$10$YvqEvGrPjC0XM0lAmhZreuuq/kgCk4l1k1r2Kc/NT/ilyIe0dZChq
            String passwordDb = entity.getPassword();
            BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
            // 2.进行密码匹配
            boolean matches = passwordEncoder.matches(password, passwordDb);
            if(matches){
                return entity;
            }else{
                return null;
            }
        }
    }

    /**
     * 社交用户登录功能
     * @param socialUser
     * @return
     * @throws Exception
     */
    @Override
    public MemberEntity login(SocialUser socialUser) throws Exception {
        //具有登录和注册合并逻辑
        String uid = socialUser.getUid();
        MemberDao memberDao = this.baseMapper;
        //判断当前社交用户是否已经登录过系统
        MemberEntity memberEntity = memberDao.selectOne(
                new QueryWrapper<MemberEntity>().eq("social_uid", uid));

        if(memberEntity != null){
            //1.这个用户已经注册，更新用户的社交信息即可
            MemberEntity update = new MemberEntity();
            update.setId(memberEntity.getId());
            update.setAccessToken(socialUser.getAccess_token());
            update.setExpiresIn(socialUser.getExpires_in());
            //更新用户的信息
            memberDao.updateById(update);

            memberEntity.setAccessToken(socialUser.getAccess_token());
            memberEntity.setExpiresIn(socialUser.getExpires_in());
            return memberEntity;
        }else{
            // 2.没有查到当前社交用户对应的记录，我们就需要注册一个
            MemberEntity regist = new MemberEntity();
           try{
               //3. 查询当前社交用户的社交账号信息（昵称，性别等）
               Map<String, String> query = new HashMap<>();
               query.put("access_token",socialUser.getAccess_token());
               query.put("uid",socialUser.getUid());
               HttpResponse response = HttpUtils.doGet("https://api.weibo.com",
                       "/2/users/show.json",
                       "get", new HashMap<String, String>(), query);
               if(response.getStatusLine().getStatusCode() == 200){
                   //查询成功
                   String json = EntityUtils.toString(response.getEntity());
                   JSONObject jsonObject = JSON.parseObject(json);
                   //昵称
                   String name = jsonObject.getString("name");
                   //性别
                   String gender = jsonObject.getString("gender");
                   //.........
                   regist.setNickname(name);
                   regist.setGender("m".equals(gender)?1:0);
                   //.........
                   }
           }catch(Exception e){}

            regist.setSocialUid(socialUser.getUid());
            regist.setAccessToken(socialUser.getAccess_token());
            regist.setExpiresIn(socialUser.getExpires_in());
            memberDao.insert(regist);
            return regist;
        }
    }

}